package com.zhanghp.demo01;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhanghp
 * @date 2022-08-26 14:03
 */
@Slf4j
public class Child {

    private ActionType actionType;

    private final List<ChildObserver> OBSERVERS;

    public Child() {
        actionType = ActionType.SLEEPING;
        OBSERVERS = new ArrayList<>();
    }

    public void add(ChildObserver obs){
        OBSERVERS.add(obs);
    }

    public void remove(ChildObserver obs){
        OBSERVERS.remove(obs);
    }

    public void changeAction(){
        var actionTypes = ActionType.values();
        actionType = actionTypes[(actionType.ordinal() + 1) % actionTypes.length];
        log.info("the child is {}", actionType);
        notifyObservers();
    }

    public void notifyObservers(){
        for (var observer : OBSERVERS) {
            observer.update(actionType);
        }
    }
}
